<script setup>
import { ref } from "vue";
import { useemployeeInfoStore } from "@/src-manager/stores/employeeInfo.js";
import { ElMessage } from "element-plus";
import router from '@/src-manager/router/index.js';

const employeeInfoStore = useemployeeInfoStore();

const employeeInfo = ref({
  ...employeeInfoStore.info
});

const employeePassword = ref({
  old_pwd: "",
  new_pwd: "",
  re_pwd: "",
});

// 定义表单校验规则
const checkRePassword = (rule, value, callback) => {
  if (value === "") {
    callback(new Error("请再次确认密码"));
  } else if (value !== employeePassword.value.new_pwd) {
    callback(new Error("请确保两次输入密码一致"));
  } else {
    callback(); //代表校验通过
  }
};

// 定义表单校验规则
const rulesofPsw = {
  old_pwd: [{ required: true, message: "请输入密码", trigger: "blur" }],

  new_pwd: [
    { required: true, message: "请输入密码", trigger: "blur" },
    { min: 5, max: 16, message: "长度为5~16位非空字符", trigger: "blur" },
  ],
  re_pwd: [
    { required: true, message: "请输入密码", trigger: "blur" },
    { validator: checkRePassword, trigger: "blur" },
  ],
};
import { updatePasswordService } from "@/src-manager/api/employee.js";

const updatePassword = async () => {

  let params = new URLSearchParams();
  params.append("oldPassword", employeePassword.value.old_pwd);
  params.append("newPassword", employeePassword.value.new_pwd);
  params.append("rePassword", employeePassword.value.re_pwd);

  console.log(params.toString());


  let result = await updatePasswordService(params);

  ElMessage.success(result.msg ? result.msg : "修改密码成功");
  employeeInfoStore.setInfo(employeeInfo.value);
  router.push("/manager/emplogin")
};
</script>
<template>
  <el-card class="page-container">
    <template #header>
      <div class="header">
        <span>基本资料</span>
      </div>
    </template>
    <el-row>
      <el-col :span="12">
        <el-form :model="employeePassword" :rules="rulesofPsw" label-width="100px" size="large">
          <el-form-item label="登录名称">
            <el-input v-model="employeeInfo.employeeName" disabled></el-input>
          </el-form-item>
          <el-form-item label="原密码" prop="old_pwd">
            <el-input type="password" v-model="employeePassword.old_pwd" show-password></el-input>
          </el-form-item>
          <el-form-item label="新密码" prop="new_pwd">
            <el-input type="password" v-model="employeePassword.new_pwd" show-password></el-input>
          </el-form-item>
          <el-form-item label="确认新密码" prop="re_pwd">
            <el-input v-model="employeePassword.re_pwd" show-password></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="updatePassword">提交修改</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </el-card>
</template>
